

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@WebServlet("/Barracks")
public class Barracks extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public Barracks() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession ses = request.getSession();
		Integer USER_ID = (Integer) ses.getAttribute("USER_ID");
		Integer P_ADDRESS = (Integer) ses.getAttribute("P_ADDRESS");
		
		if(USER_ID==null||P_ADDRESS==null){
			response.sendRedirect("Overview");
			return;
		}
		
		Database baza = null;
		ResultSet rs = null;
		
		ArrayList<Integer> tUNIT_ID = new ArrayList<>();
		ArrayList<Integer> tMove = new ArrayList<>();
		ArrayList<Integer> tCost = new ArrayList<>();
		ArrayList<Integer>	tCap = new ArrayList<>();
		ArrayList<Double> tCombat = new ArrayList<>();
		ArrayList<String> tName = new ArrayList<>();
		ArrayList<Integer> tCount = new ArrayList<>();
		
		try {
			baza = new Database();
			rs = baza.Select("*").From("UNIT_TYPES").GetResult();
			
			while(rs.next()){
				tUNIT_ID.add(rs.getInt("UNIT_ID"));
				tMove.add(rs.getInt("MOVE_SPEED"));
				tCost.add(rs.getInt("COST"));
				tCap.add(rs.getInt("CARRY_CAPACITY"));
				tCombat.add(rs.getDouble("COMBAT_STATS"));
				tName.add(rs.getString("NAME"));
				tCount.add(0);
			}
			baza.Close();
			baza = new Database();
			rs = baza.Select("*").From("POSITION_ARMY").Where("P_ADDRESS = " + P_ADDRESS).GetResult();
			while(rs.next()){
				Integer ID = rs.getInt("UNIT_ID");
				int index = tUNIT_ID.indexOf(ID);
				tCount.set(index, rs.getInt("COUNT"));
			}
			baza.Close();
			
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.setAttribute("tUnits", tUNIT_ID);
		request.setAttribute("tMove", tMove);
		request.setAttribute("tCost", tCost);
		request.setAttribute("tCap", tCap);
		request.setAttribute("tCombat", tCombat);
		request.setAttribute("tName", tName);
		request.setAttribute("tCount", tCount);
		request.getRequestDispatcher("Barracks.jsp").forward(request, response);
	}

	public static int get_unit_cost(int unit_id, int count) throws SQLException{
		Database baza = new Database();
		ResultSet rs = baza.Select("*").From("UNIT_TYPES").Where("UNIT_ID = " + unit_id).GetResult();
		rs.next();
		int unit_cost = rs.getInt("COST");
		rs.close();
		baza.Close();
		int total_cost = unit_cost*count;
		return total_cost;
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession ses = request.getSession();
		Integer USER_ID = (Integer) ses.getAttribute("USER_ID");
		Integer P_ADDRESS = (Integer) ses.getAttribute("P_ADDRESS");
		
		if(USER_ID==null||P_ADDRESS==null){
			response.sendRedirect("Overview");
			return;
		}
		
		Database baza = null;
		Database baza2 = null;
		
		Integer UNIT_ID = Integer.parseInt(request.getParameter("ID"));
		Integer Count = Integer.parseInt(request.getParameter("count"));
		ResultSet rs = null;
		ResultSet rs2 = null;
		Integer ArmyID = null;
		Integer ResID = null;
		Integer Quantity = null;
		Integer Cost = null;
		
		try {
			baza = new Database();
			rs = baza.Select("*").From("POSITION").Where("P_ADDRESS = " + P_ADDRESS).GetResult();
			rs.next();
			ArmyID = rs.getInt("ARMY_ID");
			ResID = rs.getInt("RES_ID");
			baza.Close();
			rs.close();
			
			Integer Resources = Overview.get_current_resources(P_ADDRESS);
			baza = new Database();
			rs = baza.Select("*").From("UNITS").Where("UNIT_ID = "+ UNIT_ID + 
					" AND ARMY_ID = " + ArmyID).GetResult();
			Cost=get_unit_cost(UNIT_ID, Count);
			if(Resources > Cost){
				Resources -= Cost;
				int resID = Overview.get_resID(P_ADDRESS);
				Overview.update_resources(Resources, resID);
				ses.setAttribute("Resources", Resources);
				if(rs.isBeforeFirst()){
					rs.next();
					Integer prevCount = rs.getInt("COUNT");
					baza.Close();
					baza = new Database();
					baza.Update("UNITS").Set("COUNT = " + (prevCount+Count)).Where("UNIT_ID = "+ UNIT_ID + 
							" AND ARMY_ID = " + ArmyID).execute();
					baza.Close();
					
				} else {
					baza.Close();
					baza = new Database();
					baza.InsertInto("UNITS").Values(UNIT_ID + ", " + ArmyID + ", " + Count).execute();
					baza.Close();
				}
			} else {
				response.sendRedirect("Barracks");
				return;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		response.sendRedirect("Barracks");
		return;
	}

}
